package admin.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.CommonUtil;
import com.common.Dateutil;
import com.common.ResultData;
import com.filter.BaseController;
import com.intercepter.AuthPassport;

import beans.sys.UserBean;
import beans.user.WithdrawcashSearch;
import beans.user.user;
import beans.user.withdrawcash;
import service.user.WXUserServiceImpl;
import service.user.WithdrawcashServiceImpl;

@Controller
@RequestMapping("/withdrawcash")
public class WithDrawCashController extends BaseController{
	@Resource
	private WXUserServiceImpl<user> wxUserServiceImpl;

	@Resource
	private WithdrawcashServiceImpl<withdrawcash> withdrawcashServiceImpl;

	@RequestMapping("/index")
	public String index() {
		return "admin/withdrawcash/index";
	}

	@RequestMapping("/GetData")
	@ResponseBody
	@AuthPassport(actionName="index",controllerName="wxadminuser")
	public void DataGrid(WithdrawcashSearch model){
		ResultData<List<withdrawcash>> rd = new ResultData<>();
		try{
			rd.setPage(model.getPage());

			if(!CommonUtil.isNullObject(model.getEndtime())){
				model.setEndtime(Dateutil.nextDate(model.getEndtime()));
			}
			List<withdrawcash> m = withdrawcashServiceImpl.queryByList(model);
			String[] ids = new String[m.size()];
			int i=0;
			for(withdrawcash with : m) {
				ids[i] = with.getUid();
				i++;
			}
			if(ids.length>0) {
				List<user> userList = wxUserServiceImpl.queryByIds(ids);
				for(withdrawcash with : m) {
					for(user tem : userList) {
						if(with.getUid().equals(tem.getOpenid())) {
							with.setUname(tem.getNickname());
							break;
						}
					}
				}
			}

			rd.setRows(m);
			rd.setRecords(model.getPager().getRowCount());
			rd.setTotal(model.getPager().getPageCount());
		}
		catch(Exception e){
			e.printStackTrace();
		}
		responseJson(rd);
	}

	@RequestMapping("/detail")
	public String detail(String id,String status) {
		withdrawcash widthdraw = null;
		try {
			widthdraw = withdrawcashServiceImpl.queryById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		getReq().setAttribute("widthdraw", widthdraw);
		getReq().setAttribute("status", status);
		return "admin/withdrawcash/detail";
	}

	@ResponseBody
	@RequestMapping("/save")
	public void save(withdrawcash m) {
		ResultData<String> rd = new ResultData<>();
		try {
			withdrawcash widthdraw = withdrawcashServiceImpl.queryById(m.getId());
			if(!widthdraw.getStatus().equals("1")) {
				rd.setMessage("请确认该申请是否是新申请!");
				responseJson(rd);
				return;
			}
			
			UserBean u = (UserBean)getReq().getSession().getAttribute("user");
			m.setReturntime(new Date());
			m.setReturnuser(u.getId());
			
			withdrawcashServiceImpl.update(m);
			rd.setType(1);
			rd.setMessage("保存成功!");
		} catch (Exception e) {
			rd.setMessage("系统错误,请联系管理员!");
			e.printStackTrace();
		}
		responseJson(rd);
	}
}
